using ImageCSharp.Core.Interfaces;
using System.Collections.ObjectModel;
using ImageCSharp.Core.Models;
using CommunityToolkit.Mvvm.Messaging;
using ImageCSharp.Core.Events;

namespace ImageCSharp.Core.Services
{
    /// <summary>
    /// 历史记录服务实现
    /// </summary>
    public class HistoryService : IHistoryService
    {
        private readonly ObservableCollection<HistoryItem> _history = new();
        private int _currentIndex = -1;
        private const int MaxHistoryItems = 50;

        public void AddHistoryItem(string action, ImageDocument state)
        {
            // 如果当前不是最新状态，删除后面的历史记录
            if (_currentIndex < _history.Count - 1)
            {
                for (int i = _history.Count - 1; i > _currentIndex; i--)
                {
                    _history.RemoveAt(i);
                }
            }

            // 如果达到最大历史记录数，删除最旧的
            if (_history.Count >= MaxHistoryItems)
            {
                _history.RemoveAt(0);
                _currentIndex--;
            }

            // 添加新的历史记录项
            var historyItem = new HistoryItem
            {
                Action = action,
                Timestamp = DateTime.Now,
                State = state.Clone(),
                IsActive = true,
                Icon = GetIconForAction(action)
            };

            _history.Add(historyItem);
            _currentIndex = _history.Count - 1;

            // 更新活动状态
            UpdateActiveStates();
            
            // 发送历史记录变更事件
            WeakReferenceMessenger.Default.Send(new HistoryChangedEvent($"Added history: {action}"));
        }

        private string GetIconForAction(string action)
        {
            if (action.Contains("滤镜")) return "Filter";
            if (action.Contains("调整大小")) return "Resize";
            if (action.Contains("旋转")) return "Rotate";
            if (action.Contains("打开")) return "FileOpen";
            if (action.Contains("保存")) return "ContentSave";
            if (action.Contains("绘制")) return "Brush";
            if (action.Contains("选择")) return "CursorDefault";
            return "Help";
        }

        public bool Undo()
        {
            if (!CanUndo)
                return false;

            _currentIndex--;
            UpdateActiveStates();
            
            // 发送历史记录变更事件
            WeakReferenceMessenger.Default.Send(new HistoryChangedEvent("Undo operation"));
            
            return true;
        }

        public bool Redo()
        {
            if (!CanRedo)
                return false;

            _currentIndex++;
            UpdateActiveStates();
            
            // 发送历史记录变更事件
            WeakReferenceMessenger.Default.Send(new HistoryChangedEvent("Redo operation"));
            
            return true;
        }

        public ObservableCollection<HistoryItem> GetHistory()
        {
            return _history;
        }

        public void Clear()
        {
            _history.Clear();
            _currentIndex = -1;
        }

        public bool CanUndo => _currentIndex > 0;

        public bool CanRedo => _currentIndex >= 0 && _currentIndex < _history.Count - 1;

        private void UpdateActiveStates()
        {
            for (int i = 0; i < _history.Count; i++)
            {
                _history[i].IsActive = i == _currentIndex;
            }
        }
    }
}